/**
 * Created by admin on 2017/7/13.
 */
(function () {
    var module = {};
    var tools = Tools();
    var custodyStatus;

    module.init=function () {
        tools.isOpenAccount();
        tools.getUserInfo();
        //查询用户名
        tools.doGet(accountURL + tools.apiURL.queryUserInfo, {}, module.queryUserInfo);
        /*    //获取银行卡信息
         tools.doGet(accountURL+tools.apiURL.bankCard,{},module.bankCard);*/
        //测试结论
        tools.doGet(accountURL+tools.apiURL.testResult,{},module.testResult);
        module.checkAll();




    };

    function isEmptyObject(e) {
        var t;
        for (t in e)
            return !1;
        return !0
    }

    // 0:存在 ；其他都是不存在 交易密码
    module.tradePasswordExists=function (data) {
        if(custodyStatus==1){
            if(data.status==0){
                $('#unPassword').hide();
                $('#upSet').hide();
                $('.jyPassword').append('<span>修改</span>');
                $('#setTradePw').append('<form  id="J_ChangeTradePassword" name="changeTradePassword" novalidate="novalidate"> <div class="formClass"> <ul> <li><label class="inputName">旧交易密码：</label> <input type="password" id="J_OldTradePassword" name="oldTradePassword" class="inputText" maxlength="6" style="color: rgb(0, 0, 0);"></li> <li><label class="inputName">新的交易密码：</label> <input type="password" id="J_NewTradePassword" name="newTradePassword" class="inputText" maxlength="6" style="color: rgb(0, 0, 0);"></li> <li><label class="inputName">确认新的交易密码：</label> <input type="password" id="J_ConfirmTradePassword" name="confirmTradePassword" class="inputText" maxlength="6" style="color: rgb(0, 0, 0);"> </li> <li><input type="submit" id="J_ChangeTradePasswordSubmit" class="inputBut showL" value="确定修改"></li> </ul> </div> </form> ');

            }else{
                $('#setTradePw').append('<form  id="J_SetTradePassword" name="setTradePassword"> <div class="formClass"> <ul> <li><label class="inputName">交易密码：</label> <input type="password" id="J_TradePassword" name="tradePassword" class="inputText"></li> <li><input type="submit" id="J_SetTradePasswordSubmit" class="inputBut showL" value="提交设置"></li> </ul> </div> </form> ');
                $('#unPassword').show();
                $('.jyPassword').append('<span id="upSet">设置</span>');
            }
        }else{
            if(data.status==0){
                $('#unPassword').hide();
                $('#upSet').hide();
                $('.jyPassword').append('<span class="upSet">修改</span>');
            }else{
                $('#unPassword').show();
                $('.jyPassword').append('<span class="upSet">设置</span>');
            }
            $(document).on("click", ".upSet", function () {
                $('body').after('<div class="pop_error_oa goOpenAccount"><p class="pop_error_oa error_oa"></p> <div class="pop_oa"> <div > <a href="javascript:;" target="_blank" class="pop_op openAccount_open">存管开户</a> <p class="pop_fg"></p> <a href="javascript:;" class="pop_wait openAccount_close">以后再说</a> </div> </div> </div>');
            });
            //开户弹窗   点击俩按钮
            $(document).on("click", ".openAccount_open", function () {
                $('.openAccount_open').attr('href', 'openAccount.html')
            });
            $(document).on("click", ".openAccount_close", function () {
                $('.goOpenAccount').hide();
            });
        }
        module.checkAll();
    };
    module.testResult=function (data) {
        if(data.status==0){
            if(isEmptyObject(data.data)){
                $('#textInfo').text('快来测一测，让您的投资聪明起来！');
                $('#textGo').append('<span>马上测试</span>')
            }else{
                if(data.data.times==0){
                    $('#textInfo').text('快来测一测，让您的投资聪明起来！');
                    $('#textGo').append('<span>马上测试</span>')
                }else if(data.data.times==1){
                    $('#textInfo').text(data.data.result);
                    $('#textGo').append('<span>重新测试</span>')
                }else{
                    $('#textGo').hide();
                    $('#textInfo').text(data.data.result);
                }
            }
        }
    };

    /* module.bankCard=function (data) {
     if (data.status==0) {
     if (data.data != null) {

     }
     }
     };*/


    //查询用户信息
    module.queryUserInfo = function (data) {
        if(data.status==0){
            //用户名
            $('#userName').text(data.data.username);
            var content = data.data.custodyStatus;
            custodyStatus=data.data.custodyStatus;
            if(content==1){
                $('#isIdent').show();
                $('#unIdent').hide();
                $('#cgUpdateInfo').text('修改');
                $(document).on('click','.cgUpdatePassword',function () {
                    toNewPagePw();
                })
            }else{
                $('#isIdent').hide();
                $('#unIdent').show();
                $('#toSafetyBank').text('未开户');
                $('#openOrBank,.cgUpdatePassword').attr('href','openAccount.html');
                $('#cgUpdateInfo').text('设置')
            }
        }else{
            $('#isIdent').hide();
            $('#unIdent').show();
            $('#toSafetyBank').text('未认证');
            $('#openOrBank').attr('href','openAccount.html')
        }
        //交易密码是否存在
        tools.doPost(accountURL +tools.apiURL.tradePasswordExists,{},module.tradePasswordExists);
    };

    function toNewPagePw() {
        var UrLs='any.html';
        var money=$('#wy_money').val();
        var newWindow = window.open(UrLs+'?newPage=Pw');

    }
    module.checkAll=function () {
        $changePwdForm = $("#J_ChangePassword");
        $changePwdSubmit = $("#J_ChangePasswordSubmit");
        $password = $("#J_NewPassword");
        $oldPassword = $("#J_OldPassword");


        $setTradePwdForm = $("#J_SetTradePassword");
        $setTradePwdSubmit = $("#J_SetTradePasswordSubmit");
        $tradePassword = $("#J_TradePassword");

        $changeTradePwdForm = $("#J_ChangeTradePassword");
        $changeTradePwdSubmit = $("#J_ChangeTradePasswordSubmit");
        $newTradePassword = $("#J_NewTradePassword");
        $oldTradePassword = $("#J_OldTradePassword");

        $.validator.addMethod("notEqualTo", function (value, element, param) {
            if ($(param).val() == value) {
                return false;
            } else {
                return true;
            }
        });

        $.validator.addMethod("validIdentity", function (value, element) {
            return idCardValidate(value);
        });

        // 修改登录密码表单验证
        $changePwdForm.validate({
            rules: {
                oldPassword: "required",
                newPassword: {
                    required: true,
                    pattern: /^[^\s&\"<>]+$/,
                    minlength: 6,
                    maxlength: 18,
                    notEqualTo: $oldPassword
                },
                confirmPassword: {
                    required: true,
                    equalTo: "#J_NewPassword"
                }
            },
            messages: {
                oldPassword: {
                    required: "您还未填写原登陆密码"
                },
                newPassword: {
                    required: "您还未填写新的登陆密码",
                    pattern: "密码不能包含下述字符：<>\"&",
                    minlength: "密码长度必须是6~18位",
                    maxlength: "密码长度必须是6~18位",
                    notEqualTo: "新密码不能和原密码相同，请重新输入"
                },
                confirmPassword: {
                    required: "您还未确认填写新的登陆密码",
                    equalTo: "新密码和确认密码不一致"
                }
            },
            submitHandler: function () {
                tools.doPost(accountURL +tools.apiURL.updateUserPwd, {
                    oldUserPwd: $("#J_OldPassword").val(),
                    newUserPwd: $("#J_NewPassword").val()
                }, module.updateUserPwd);
            }
        });
        // 设置交易密码表单验证
        $setTradePwdForm.validate({
            rules: {
                tradePassword: {
                    required: true,
                    pattern: /^\d{6}$/,
                    minlength: 6
                }
            },
            messages: {
                tradePassword: {
                    required: "您还未填写交易密码",
                    pattern: "交易密码只能为六位数字",
                    minlength: "交易密码长度必须为六位"
                }
            },
            submitHandler: function (form) {
                tools.doPost(accountURL +tools.apiURL.setTradePassword, {tradePassword:$("#J_TradePassword").val()}, module.checkTradePassword);
            }
        });


        // 修改交易密码表单验证
        $changeTradePwdForm.validate({
            rules: {
                oldTradePassword: "required",
                newTradePassword: {
                    required: true,
                    pattern: /^\d{6}$/,
                    minlength: 6,
                    notEqualTo: $oldTradePassword
                },
                confirmTradePassword: {
                    required: true,
                    equalTo: "#J_NewTradePassword"
                }
            },
            messages: {
                oldTradePassword: {
                    required: "您还未填写原交易密码"
                },
                newTradePassword: {
                    required: "您还未填写新的交易密码",
                    pattern: "交易密码只能为六位数字",
                    minlength: "密码长度必须为六位",
                    notEqualTo: "新密码不能和原密码相同，请重新输入"
                },
                confirmTradePassword: {
                    required: "您还未确认填写新的交易密码",
                    equalTo: "新密码和确认密码不一致"
                }
            },
            submitHandler: function (form) {
                tools.doPost(accountURL + tools.apiURL.resetTradePassword, {oldTradePassword:$('#J_OldTradePassword').val(),newTradePassword:$("#J_NewTradePassword").val()}, module.resetTradePassword);
            }
        });
    };
    module.resetTradePassword=function (data) {
        if(data.status==0){
            alert('');
        }else{
            alert(data.message);
            $("#J_TradePassword").val('');
        }
    };
    module.updateUserPwd=function (data) {
        if(data.status==0){
            alert('密码已更新，请保管好您的新密码！');
            $('.updatePsHide').hide();
        }else{
            alert(data.message);
        }
        $("#J_OldPassword").val('');
        $("#J_NewPassword").val('');
        $("#J_ConfirmPassword").val('');
    };
    module.checkTradePassword=function (data) {
        if(data.status==0){
            alert('密码设置成功，请保管好您的密码！');
            $('#setTradePw').hide();
            $('#setTradePw').html("");
            tools.doPost(accountURL +tools.apiURL.tradePasswordExists,{},module.tradePasswordExists);
        }else{
            alert(data.message);
            $("#J_TradePassword").val('');
        }
    };

    module.init();
})();
